#include <bits/stdc++.h>
using namespace std;

vector<int> findMinHeightTrees(int n, vector<vector<int>>& edges) {
    if(n == 1) return {0};
    vector<int> degree(n);
    map<int, vector<int>> map;
    for(int i = 0; i < edges.size(); ++i) {
        degree[edges[i][0]]++;
        degree[edges[i][1]]++;
        map[edges[i][0]].push_back(edges[i][1]);
        map[edges[i][1]].push_back(edges[i][0]);
    }
    queue<int> q;
    for(int i = 0; i < n; ++i) {
        if(degree[i] == 1) q.push(i);
    }
    vector<int> res;
    while(!q.empty()) {
        int sz = q.size();
        res.clear();
        for(int i = 0; i < sz; ++i) {
            int cur = q.front();
            q.pop();
            res.push_back(cur);
            degree[cur]--;
            for(auto j : map[cur]) {
                degree[j]--;
                if(degree[j] == 1) {
                    q.push(j);
                }
            }
        }
    }
    return res;
}